/*
 *所有关于MES_ProductBind类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*MES_ProductBindService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using SkiaSharp;
using System.Linq;
using System.Linq.Expressions;
using VolPro.Core;
using VolPro.Core.BaseProvider;
using VolPro.Core.Configuration;
using VolPro.Core.Enums;
using VolPro.Core.Extensions;
using VolPro.Core.Extensions.AutofacManager;
using VolPro.Core.ManageUser;
using VolPro.Core.Services;
using VolPro.Core.Utilities;
using VolPro.Entity.DomainModels;
using VolPro.MES.IRepositories;

namespace VolPro.MES.Services
{
    public partial class MES_ProductBindService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IMES_ProductBindRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public MES_ProductBindService(
            IMES_ProductBindRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent responseContent = new WebResponseContent();
        public async Task<WebResponseContent> MES_SaveAssembly(List<dynamic> liskObj)
        {
            try
            {
                MES_ProductBind mES_ProductBind;
                foreach (var item in liskObj)
                {
                    mES_ProductBind = new MES_ProductBind();
                    mES_ProductBind.SerialNumber = item.SerialNumber;
                    mES_ProductBind.OrderCode = item.OrderCode;
                    mES_ProductBind.OPName = item.OPName;
                    mES_ProductBind.ProductType = item.ProductType;
                    mES_ProductBind.ProgramNumber = item.ProgramNumber;
                    mES_ProductBind.Pallet = item.Pallet;
                    mES_ProductBind.Barcode = item.Barcode;
                    mES_ProductBind.MaterialBatch = item.MaterialBatch;
                    mES_ProductBind.MaterialCode = item.MaterialCode;
                    mES_ProductBind.ScanNumber = item.ScanNumber;
                    mES_ProductBind.ScanMode = item.ScanMode;
                    mES_ProductBind.SupplierCode = item.SupplierCode;
                    mES_ProductBind.Description = item.Description;
                    mES_ProductBind.CreateDate = DateTime.Now;
                    mES_ProductBind.CreateID = UserContext.Current.UserId;
                    mES_ProductBind.Creator = UserContext.Current.UserName;
                    repository.Add(mES_ProductBind);
                }

                responseContent.Code = "200";
                responseContent.OK("成功");
            }
            catch (Exception ex)
            {
                responseContent.Code = "500";
                responseContent.Error(ex.Message + ex.StackTrace);
                ////if (_context.GetService<Microsoft.AspNetCore.Hosting.IWebHostEnvironment>().IsDevelopment())
                ////{
                //throw new Exception(ex.Message + ex.StackTrace);
                ////}
                ////return webResponse.Error(ResponseType.ServerError);
            }
            finally
            {
                ////if (!string.IsNullOrEmpty(loginInfo.UUID))
                ////{
                ////    memoryCache.Remove(loginInfo.UUID);
                ////}
                Logger.Info(LoggerType.Login, responseContent.Message, null);
            }
            return responseContent;
        }
        // 检查属性是否存在的辅助方法
        private string PropertyExists(dynamic obj, string propertyName)
        {
            string rs =string.Empty;
            try
            {
                if (((IDictionary<string, object>)obj).ContainsKey(propertyName))
                    rs = (string)obj[propertyName];
            }
            catch (Exception)
            {
            }
            return rs;
        }
    }
}
